
/**
 * 商品列表
 */
supermarketApp.directive("goodsTable", function () {
    return {
        restrict: 'CA',
        scope: {
            data: '=',
            loading: '='
        },
        templateUrl: 'html/goods-table.html',
        link: function (scope, element, attrs) {

            scope.pageChanged = pageChanged;


            // scope.$watch('data', function () {
            //     G.d(scope.data);
            // });

            /** 跳转到指定页 **/
            scope.$watch('goToPageNumber',  function(newValue, oldValue,obj,a) {/** 数字验证 **/
                if(newValue){
                    if(scope.goToPageNumber){
                        if(scope.goToPageNumber>scope.data.totalPages){
                            /*scope.goToPageNumber=scope.data.totalPages;
                             if(scope.goToPageNumber!="0")
                             scope.goToPageNumberValidate="超出最大页数，当前共"+scope.data.totalPages+"页";*/
                        }else{
                            var num=scope.goToPageNumber+"";
                            num=num.replace(/[^\d.]/g,"").split(".")[0];
                            if(/\D/.test(newValue)){
                                scope.goToPageNumberValidate="请输入数字！";
                                scope.goPageNumberKey=1;
                            }else{
                                if(scope.goPageNumberKey==0)
                                    scope.goToPageNumberValidate="";
                            }
                            scope.goToPageNumber=num;
                        }
                    }
                }
                scope.goToPageNumber=scope.goToPageNumber==0?1:scope.goToPageNumber;
            });
            scope.goToPage=function(){
                if(scope.goToPageNumber>scope.data.totalPages){
                    scope.goToPageNumber=scope.data.totalPages;
                    if(scope.goToPageNumber!="0")
                        scope.goToPageNumberValidate="超出最大页数，当前共"+scope.data.totalPages+"页";
                }
                if(scope.data.page!=scope.goToPageNumber){
                    scope.data.page=scope.goToPageNumber;
                    pageChanged();
                }
            }
            scope.enterGoToPage=function(event){
                var keycode = window.event?event.keyCode:event.which;
                if(keycode==13){
                    scope.goToPage();
                }
            }

            function pageChanged() {
                //G.d(arguments);
                scope.data.callback(scope.data.page);
                scope.goToPageNumber=scope.data.page==0?1:scope.data.page;
            }
        }
    };
});